package cn.itcast.bos.web.action.take_delivery;


import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

	// 属性驱动 注入上传文件需要时的相关的内用
	private File titleImgFile;
	private String titleImgFileFileName;
	private String titleImgFileType;


	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}

	public void setTitleImgFileType(String titleImgFileType) {
		this.titleImgFileType = titleImgFileType;
	}

	@Autowired
	private PromotionService promotionService;

	@Action(value = "promotion_save", results = { @Result(name = "success", type = "redirect",
			location="./pages/take_delivery/promotion.html") })
	public String save() throws IOException {
		System.out.println(titleImgFileFileName);
		// 获取上传的路径 绝对路径
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");

		// 相对路径
		String saveUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
		// 生成随机文件名
		UUID uuid = UUID.randomUUID();
		// 获取文件扩展名
		String ext = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
		String randomFileName = uuid + ext;
		System.out.println("表单提交randomFileName。。。。。。。。"+randomFileName);
		//上传图片
		FileUtils.copyFile(titleImgFile, new File(savePath+"/"+randomFileName));
		model.setTitleImg(saveUrl+randomFileName);
		//文件上传响应数据
		Map<String,Object> result  =new HashMap<String,Object>();
		result.put("error", 0);
		result.put("url", saveUrl+randomFileName);
		ActionContext.getContext().getValueStack().push(result);
		promotionService.save(model);
		return SUCCESS;
	}
	
	//分页功能的实现
	@Action(value="promotion_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Promotion> pageDate = promotionService.findPageDate(pageable);
		System.out.println(pageDate.getContent()+"----------");
		pushPageDataToValueStack(pageDate);
		return SUCCESS;
	}
}
